#include "addmodifysupplier.h"
#include "ui_addmodifysupplier.h"
#include <QMessageBox>
#include "inquiresql.h"
#include "supplier.h"
#include <QDebug>
#include "guicontainerform.h"
#include <QKeyEvent>

AddModifySupplier::AddModifySupplier(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddModifySupplier)
{
    ui->setupUi(this);
//    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );
//    this->move((QApplication::desktop()->width() - this->width())/2,
//               (QApplication::desktop()->height() - this->height())/2);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(364, 433);
    ui->labelPrompt->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_sql = new InquireSql;

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;

    ui->lineEditOwnerName->installEventFilter(this);
    ui->lineEditContactName->installEventFilter(this);
    ui->lineEditTel->installEventFilter(this);
    ui->lineEditEmail->installEventFilter(this);
    ui->lineEditBankAccount->installEventFilter(this);
    ui->lineEditAdress->installEventFilter(this);
    ui->textEditRemark->installEventFilter(this);
    ui->pushButtonSave->installEventFilter(this);
}

AddModifySupplier::~AddModifySupplier()
{
    delete ui;
}

bool AddModifySupplier::eventFilter(QObject *target, QEvent *event)
{
    if (target == ui->lineEditOwnerName || target == ui->lineEditContactName
            || target == ui->lineEditTel || target == ui->lineEditEmail
            || target == ui->lineEditBankAccount || target == ui->lineEditAdress
            || target == ui->textEditRemark || target == ui->pushButtonSave)
    {
        //鼠标左键按下事件
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::LeftButton)
        {
            if (target == ui->lineEditOwnerName)
            {
                if (m_lastClick != lineEditOwnerName){
                    m_lastClick = lineEditOwnerName;
                    ui->lineEditOwnerName->selectAll();
                    return true;
                }else if (m_lastClick == lineEditOwnerName)
                {
                    goto flag;
                }
            }
            if (target == ui->lineEditContactName)
            {
                if (m_lastClick != lineEditContactName){
                    m_lastClick = lineEditContactName;
                    ui->lineEditContactName->selectAll();
                    return true;
                }else if (m_lastClick == lineEditContactName)
                {
                    goto flag;
                }
            }
            if (target == ui->lineEditTel)
            {
                if (m_lastClick != lineEditTel){
                    m_lastClick = lineEditTel;
                    ui->lineEditTel->selectAll();
                    return true;
                }else if (m_lastClick == lineEditTel)
                {
                    goto flag;
                }
            }
            if (target == ui->lineEditEmail)
            {
                if (m_lastClick != lineEditEmail){
                    m_lastClick = lineEditEmail;
                    ui->lineEditEmail->selectAll();
                    return true;
                }else if (m_lastClick == lineEditEmail)
                {
                    goto flag;
                }
            }
            if (target == ui->lineEditBankAccount)
            {
                if (m_lastClick != lineEditBankAccount){
                    m_lastClick = lineEditBankAccount;
                    ui->lineEditBankAccount->selectAll();
                    return true;
                }else if (m_lastClick == lineEditBankAccount)
                {
                    goto flag;
                }
            }
            if (target == ui->lineEditAdress)
            {
                if (m_lastClick != lineEditAdress){
                    m_lastClick = lineEditAdress;
                    ui->lineEditAdress->selectAll();
                    return true;
                }else if (m_lastClick == lineEditAdress)
                {
                    goto flag;
                }
            }
            if (target == ui->textEditRemark)
            {
                if (m_lastClick != textEditRemark){
                    m_lastClick = textEditRemark;
                    ui->textEditRemark->selectAll();
                    return true;
                }else if (m_lastClick == textEditRemark)
                {
                    goto flag;
                }
            }
            if (target == ui->pushButtonSave)
            {
                m_lastClick = pushButtonSave;
            }

        }

        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditOwnerName)
                {
                    ui->lineEditContactName->setFocus();
                    ui->lineEditContactName->selectAll();
                    return true;
                }
                if (target == ui->lineEditContactName)
                {
                    ui->lineEditTel->setFocus();
                    ui->lineEditTel->selectAll();
                    return true;
                }
                if (target == ui->lineEditTel)
                {
                    ui->lineEditEmail->setFocus();
                    ui->lineEditEmail->selectAll();
                    return true;
                }
                if (target == ui->lineEditEmail)
                {
                    ui->lineEditBankAccount->setFocus();
                    ui->lineEditBankAccount->selectAll();
                    return true;
                }
                if (target == ui->lineEditBankAccount)
                {
                    ui->lineEditAdress->setFocus();
                    ui->lineEditAdress->selectAll();
                    return true;
                }
                if (target == ui->lineEditAdress)
                {
                    ui->textEditRemark->setFocus();
                    ui->textEditRemark->selectAll();
                    return true;
                }
                if (target == ui->textEditRemark)
                {
                    ui->pushButtonSave->setFocus();
                    return true;
                }
                if (target == ui->pushButtonSave)
                {
                    if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
                        on_pushButtonSave_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

bool AddModifySupplier::supplierIsEmpty(QLineEdit *lineEdit, QString msg)
{
    if (lineEdit->text().isEmpty())
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                             "\n"+msg );
        return true;
    }
    return false;
}

void AddModifySupplier::on_pushButtonSave_clicked()
{
    if (currentPage == addSupplierPage)
    {
        if (supplierIsEmpty(ui->lineEditOwnerName, tr("厂商名称不能为空")))
            return;
        if (supplierIsEmpty(ui->lineEditContactName, tr("联系人不能为空")))
            return;
        if (supplierIsEmpty(ui->lineEditTel, tr("联系电话不能为空")))
            return;
        if (supplierIsEmpty(ui->lineEditAdress, tr("联系地址不能为空")))
            return;

        //向数据库中插入供货商        
        int supplierNumber = m_list.at(m_list.count() - 1).get_personID().toInt() + 1;

        PersonSupplier supplier(QString::number(supplierNumber).rightJustified(6, '0'),
                                ui->lineEditOwnerName->text(),
                                ui->lineEditContactName->text(),
                                ui->lineEditTel->text(),
                                ui->lineEditEmail->text(),
                                ui->lineEditBankAccount->text(),
                                ui->lineEditAdress->text(),
                                ui->textEditRemark->toPlainText());

        if (!(m_sql->InsertSupplier(supplier)))
        {
            QMessageBox::critical(this, tr(" 警告！"), tr("插入供货商信息失败") );
        }

        if (!ui->checkBoxAdd->isChecked())
            this->hide();
        else
        {
            clearSupplier();
        }
        ui->lineEditOwnerName->setFocus();
    }
    if (currentPage == modifySupplierPage)
    {
//        qDebug() << "modify";
        //判断是否为空
        if (supplierIsEmpty(ui->lineEditOwnerName, tr("厂商名称不能为空")))
            return;
        if (supplierIsEmpty(ui->lineEditContactName, tr("联系人不能为空")))
            return;
        if (supplierIsEmpty(ui->lineEditTel, tr("联系电话不能为空")))
            return;
        if (supplierIsEmpty(ui->lineEditAdress, tr("联系地址不能为空")))
            return;
        //修改供货商
        PersonSupplier supplier(QString::number(m_supplierNumber).rightJustified(6, '0'),
                                ui->lineEditOwnerName->text(),
                                ui->lineEditContactName->text(),
                                ui->lineEditTel->text(),
                                ui->lineEditEmail->text(),
                                ui->lineEditBankAccount->text(),
                                ui->lineEditAdress->text(),
                                ui->textEditRemark->toPlainText());

        if (!(m_sql->UpdateSupplier(supplier)))
        {
            QMessageBox::critical(this, tr(" 警告！"), tr("修改供货商信息失败") );
        }
        this->hide();
    }
    emit displaySupplier();
}

void AddModifySupplier::clearSupplier()
{
    ui->lineEditOwnerName->setText(NULL);

    ui->lineEditContactName->setText(NULL);
    ui->lineEditTel->setText(NULL);
    ui->lineEditEmail->setText(NULL);
    ui->lineEditBankAccount->setText(NULL);
    ui->lineEditAdress->setText(NULL);
    ui->textEditRemark->setText(NULL);
}

void AddModifySupplier::slotDisplayAddModifySupplier(int page, int supplierNumber)
{
    m_list = m_sql->InquireSupplier();
    clearSupplier();

    if (page == addSupplierPage)
    {
        setWindowTitle(tr("新增供货商"));
        currentPage = addSupplierPage;
        ui->checkBoxAdd->show();        
    }
    else if(page == modifySupplierPage)
    {
        setWindowTitle(tr("修改供货商"));
        currentPage = modifySupplierPage;
        ui->checkBoxAdd->hide();
        m_supplierNumber = supplierNumber;

        //显示当前供货商信息
        for(int i = 0; i < m_list.count(); ++i)
        {
            if (m_list.at(i).get_personID().toInt() == m_supplierNumber)
            {
                ui->lineEditOwnerName->setText(m_list.at(i).get_ownerName());
                ui->lineEditContactName->setText(m_list.at(i).get_personName());
                ui->lineEditTel->setText(m_list.at(i).get_personTel());
                ui->lineEditEmail->setText(m_list.at(i).get_personEmail());
                ui->lineEditBankAccount->setText(m_list.at(i).get_supplierBankAccount());
                ui->lineEditAdress->setText(m_list.at(i).get_personAdress());
                ui->textEditRemark->setText(m_list.at(i).get_supplierRemark());
                break;
            }
        }
    }    
    ui->lineEditOwnerName->setFocus();
    ui->lineEditOwnerName->selectAll();
//    this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool);
//    this->setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::Tool);

//    this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool);
//    this->move((QApplication::desktop()->width() - this->width())/2,
//               (QApplication::desktop()->height() - this->height())/2);
//    this->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
//    this->setAttribute(Qt::WA_ShowModal, true);
//    this->resize(364, 433);
//    this->show();
}

void AddModifySupplier::on_pushButtonCancel_clicked()
{
    this->hide();
}

void AddModifySupplier::keyPressEvent(QKeyEvent *ev)
{
    switch(ev->key())
    {
        case Qt::Key_Escape:
            this->hide();
            break;
    default:
        QWidget::keyPressEvent(ev);
    }
}
